import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "../layout/index.vue"
import Login from "../views/Login.vue"
import Detail from "../views/Detail.vue"

import Erro from "../views/errPage/404.vue"
import tabRouter from "../router/page"
Vue.use(VueRouter)

//路由白名单，是不是用户都能访问
const whiteList=[
  'Detail',
  '404',
  'Login',
  'Home'
]

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Layout,
    children:tabRouter,
  },
  {
    path:'/login',
    name:'Login',
    component:Login
  },
  {
    path:'/404',
    name:'404',
    component:Erro
  },
  {
    path:'/detail',
    name:'Detail',
    component:Detail,
    befoerEnter:(to,from,next)=>{
      next()
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  const showRouter=tabRouter
  const token=localStorage.getItem('token')
  if(to.path!=='/login'&&!token){
        next('/login')
  }else{
    if(showRouter.some(item=>item.path)||whiteList.some(item=>item===to.name)){
      next()
    }else{
      next('/404')
    }
  }

})
export default router
